﻿@model Gateway.Console.Models.SecurityModel

@{
    ViewBag.Title = "Gateway Security";
}

<h2>Gateway Security</h2>

@if (ViewBag.GraphApiAuthorizationFailure)
{
    <p>
        <strong>
            This Gateway Management Console application has not been enabled for write access to the Azure Active Directory.<br />
            Please follow the directions listed in the Service Gateway documentation <a href="http://sg.codeplex.com/wikipage?title=Console">here</a> to create the necessary entries in the directory for this application to run.
        </strong>
    </p>
}
else if (ViewBag.RdfeAuthorizationFailure)
{
    using (Ajax.BeginForm("EnableRdfeAccess", "Security", new AjaxOptions() { HttpMethod = "POST", OnSuccess = "success" }, new { enctype = "multipart/form-data", role = "form" }))
    { 
        <p>
            <strong>
                Before this application can deploy the Service Gateway to Microsoft Azure, it must be authorized for access to the Microsoft Azure Management API.<br />
                Would you like to authorize this application to manage your Microsoft Azure subscription(s) on your behalf to deploy the Gateway?
            </strong>
        </p>
        <br/>
        @Html.Submit("create", "Yes, I authorize access to Microsoft Azure subscriptions", new { @class = "btn btn-default" })
    }
}
else
{ 
    using (Ajax.BeginForm("Index", "Security", new AjaxOptions() { HttpMethod = "POST", OnSuccess = "success" }, new { enctype = "multipart/form-data", role = "form" }))
    {
        @Html.HiddenFor(model => model.Id)
        @Html.HiddenFor(model => model.Directory)
        @Html.HiddenFor(model => model.ApplicationName)
        <div class="checkbox">
            @Html.CheckBoxFor(model => model.EnableAuthenticationValue, new { @class = "enable-authentication" }).Disable(() => ViewBag.GraphApiAuthorizationFailure) 
            @Html.DisplayNameFor(model => model.EnableAuthentication)
        </div>
        <br />
        // TODO: Re-enable when AAD finalizes their multi-tenant app story
        if (false)
        { 
            <div class="checkbox">
                @Html.CheckBoxFor(model => model.MultiTenantAuthenticationValue, new { @class = "authentication-entry-save", disabled = true }) 
                @Html.DisplayNameFor(model => model.MultiTenantAuthentication)
            </div>
        }
        <table class="table">
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.Directory)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.ApplicationName)
                </th>
                <th></th>
            </tr>
            <tr>
                <td>
                    @Html.DisplayFor(model => model.Directory)
                </td>
                <td>
                    @Html.DisplayFor(model => model.ApplicationName)
                </td>
                <td>
                    @Html.ActionLink("Details", "Edit", new { id = Model.Id }, new { @class = "authentication-entry-anchor" })
                </td>
            </tr>
        </table>
        @Html.Submit("create", "Save", new { @class = "btn btn-default" }).Disable(() => ViewBag.GraphApiAuthorizationFailure)
    }
}

@section Scripts {
    @if (!ViewBag.GraphApiAuthorizationFailure && !ViewBag.RdfeAuthorizationFailure)
    {
        <script type="text/javascript">
            $(document).ready(function () {
                enableAuthenticationControls();
                $('.enable-authentication').click(function (event) {
                    enableAuthenticationControls();
                });
            });

            function enableAuthenticationControls() {
                if ($('.enable-authentication').is(':checked')) {
                    $('.authentication-entry').removeAttr('disabled');
                    $('.authentication-entry-anchor').removeClass('disabled');
                    var anchor = $('.authentication-entry-anchor');
                    anchor.attr('href', anchor.attr('disabledhref'));
                    anchor.removeAttr('disabledhref');
                }
                else {
                    $('.authentication-entry').attr('disabled', true);
                    $('.authentication-entry-anchor').addClass('disabled');
                    var anchor = $('.authentication-entry-anchor');
                    anchor.attr('disabledHref', anchor.attr('href'));
                    anchor.removeAttr('href');
                }
            }
        </script>
    }
}


